select_categories = function(current_tag){
	var id = current_tag.attr("id");
	var index = parseInt(id.charAt(id.length - 1));
	var text = current_tag.find("option:selected").text();
	var parent_id = current_tag.val();
	var loop = index + 1;
	while (loop <= 6)
	{
		$("#categories_" + loop).html('<option value="0">-----------------------</option>').attr("disabled", true);
		loop++;
	}
	if ((text.charAt(text.length - 1) != '>' && parent_id > 0) || index >= 6)
	{
		$("#categories_select_id").html(parent_id);
		var loop = 1;
		var full_text = '';
		var current_text = '';
		while (loop < index)
		{
			current_text = $("#categories_" + loop + " option:selected").text();
			if (current_text.charAt(current_text.length - 1) == '>')
			{
				full_text += current_text.substr(0, current_text.length - 2) + ':';
			}
			else
			{
				full_text += $("#categories_" + loop + " option:selected").text();
			}
			loop++;
		}
		$("#categories_select_name").html(full_text + text);
		return false;
	}
	else
	{
		if (index > 0)
		{
			$("#categories_select_id").html("");
			$("#categories_select_name").html("");
		}
	}
	var level = index + 1;
	var next_id = 'categories_' + (index + 1);
	$("#" + next_id).html('<option value="0">--------Loading--------</option>').attr("disabled", false);
	$.ajax({
		url: browse_categories_ajax_url + "&site=" + $("#site").val() + "&level=" + level + "&parent_id=" + parent_id,
		type: "get",
		dataType: "json",
		success:
			function(obj){
				_html = '';
				$.each(obj, function (i, v){
					if (v.leaf == is_leaf)
					{
						_html += '<option value="' + v.id + '">' + v.name + '</option>';
					}
					else
					{
						_html += '<option value="' + v.id + '">' + v.name + ' ></option>';
					}
				});
				$("#" + next_id).html(_html);
			}
	});
};
refresh_by_shop_and_site = function(){
	$.ajax({
		url: get_info_by_shop_and_site_ajax_url + "&shop_id=" + $("#shop").val() + "&site=" + $("#site").val(),
		type: "get",
		dataType: "json",
		beforeSend: function(){
			$(".store-category-select").attr("disabled", true);
			$(".store-category-loading").html("<img src='img/loading.gif' />");
		},
		success: function(data){
			/*Categories*/
			var _html_store_categories = '<option value="">--none--</option>';
			$.each(data['store_categories'], function(k, v){
				_html_store_categories += '<option value="' + v['value'] + '">' + v['name'] + '</option>';
			});
			$(".store-category-select").html(_html_store_categories);
		},
		complete: function(){
			$(".store-category-select").attr("disabled", false);
			$(".store-category-loading").html("");
		}
	});
};
refresh_by_site = function(){
	$.ajax({
		url: get_info_by_site_ajax_url + "&site=" + $("#site").val(),
		type: "get",
		dataType: "json",
		beforeSend: function(){
			$(".price-loading").html("<img src='img/loading.gif' />");
			$(".shipping-loading").html("<img src='img/loading.gif' />");
			$(".return-policy").html("<img src='img/loading.gif' />");
		},
		success: function(data){
			/*Currency*/
			if (data['currency'])
			{
				currency_symbol = data['currency']['symbol'];
				currency_code = data['currency']['code'];
				$(".currency_symbol").text(data['currency']['symbol']);
				$(".currency_code").text(data['currency']['code']);
			}
			/*Dispatch*/
			var _html_dispatch_time = '<option value="">--none--</option>';
			$.each(data['dispatch'], function(k, v){
				_html_dispatch_time += '<option value="' + v['value'] + '">' + v['name'] + '</option>';
			});
			$("#dispatch_time_select").html(_html_dispatch_time);
			
			shipping_location = data['shipping_location'];
			var shipping_service = data['shipping_service'];
			/*Domestic Shipping*/
			domestic_shipping_service_option_html = data['shipping_service']['domestic_shipping_service_option_html'];
			var _html_domestic_shipping_service_content = '<tr>';
			_html_domestic_shipping_service_content += '<th width="70">Service</th><td align="left"><select name="domestic_shipping[add0][service]">' + domestic_shipping_service_option_html + '</select>';
			_html_domestic_shipping_service_content += ' Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="domestic_shipping[add0][cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
			_html_domestic_shipping_service_content += ' Additional Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="domestic_shipping[add0][additional_cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
			_html_domestic_shipping_service_content += ' <label><input type="checkbox" name="domestic_shipping[add0][free]" value="' + is_free + '" class="shipping-free-checkbox" />Free</label>';
			_html_domestic_shipping_service_content += '</td></tr>';
			$("#domestic_shipping_service_content").html(_html_domestic_shipping_service_content);
			
			/*International Shipping*/
			international_shipping_service_option_html = data['shipping_service']['international_shipping_service_option_html'];
			var _html_international_shipping_service_content = '<tr>';
			_html_international_shipping_service_content += '<th width="70">Service <a href="javascript:;" class="shipping-service-del-btn">Del</a></th><td align="left"><select name="international_shipping[add0][service]">' + international_shipping_service_option_html + '</select>';
			_html_international_shipping_service_content += ' Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="international_shipping[add0][cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
			_html_international_shipping_service_content += ' Additional Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="international_shipping[add0][additional_cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
			_html_international_shipping_service_content += ' <label><input type="checkbox" name="international_shipping[add0][free]" value="' + is_free + '" class="shipping-free-checkbox" />Free</label><hr style="margin: 4px 0;" />';
			_html_international_shipping_service_content += 'Location: <select name="international_shipping[add0][location]" title="add0" class="shipping-service-location-select"><option value="Worldwide">Worldwide</option><option value="Custom">Custom</option></select><br /><div class="shipping-service-location-content"></div>';
			_html_international_shipping_service_content += '</td></tr>';
			$("#international_shipping_service_content").html(_html_international_shipping_service_content);
			
			/*Return Policy*/
			var _html_return_accepted = '';
			$.each(data['return_policy']['return_accepted'], function(k, v){
				_html_return_accepted += '<label><input type="radio" name="return_policy" value="' + v['value'] + '" class="return-policy-accepted-radio" />' + v['name'] + '</label>';
			});
			$("#return_policy_accepted_content").html(_html_return_accepted);
			var _html_return_policy = '';
			if (typeof(data['return_policy']['restocking_fee']) != 'undefined')
			{
				_html_return_policy += '<tr><th>Restocking Fee Value</th><td align="left"><select name="return_option_restocking">';
				$.each(data['return_policy']['restocking_fee'], function(k, v){
					_html_return_policy += '<option value="' + v['value'] + '">' + v['name'] + '</option>';
				});
				_html_return_policy += '</select></td></tr>';
			}
			if (typeof(data['return_policy']['return_within']) != 'undefined')
			{
				_html_return_policy += '<tr><th>Return Within</th><td align="left"><select name="return_option_within">';
				$.each(data['return_policy']['return_within'], function(k, v){
					_html_return_policy += '<option value="' + v['value'] + '">' + v['name'] + '</option>';
				});
				_html_return_policy += '</select></td></tr>';
			}
			if (typeof(data['return_policy']['refund']) != 'undefined')
			{
				_html_return_policy += '<tr><th>Refund</th><td align="left"><select name="return_option_refund">';
				$.each(data['return_policy']['refund'], function(k, v){
					_html_return_policy += '<option value="' + v['value'] + '">' + v['name'] + '</option>';
				});
				_html_return_policy += '</select></td></tr>';
			}
			if (typeof(data['return_policy']['shipping_cost_paid_by']) != 'undefined')
			{
				_html_return_policy += '<tr><th>Return Shipping Paid By</th><td align="left"><select name="return_option_paid_by">';
				$.each(data['return_policy']['shipping_cost_paid_by'], function(k, v){
					_html_return_policy += '<option value="' + v['value'] + '">' + v['name'] + '</option>';
				});
				_html_return_policy += '</select></td></tr>';
			}
			if (data['return_policy']['description'])
			{
				_html_return_policy += '<tr><th>Additional Return Policy Details</th><td align="left"><textarea name="return_option_description" style="width: 500px;height: 100px;resize: none;"></textarea><br />(5000 Chars Max. No HTML)</td></tr>';
			}
			$("#return_policy_content").html(_html_return_policy);
			$(".return-policy-accepted-radio:first").click();
		},
		complete: function(){
			$(".price-loading").html("");
			$(".shipping-loading").html("");
			$(".return-policy").html("");
		}
	});
};
refresh_by_category = function(){
	$.ajax({
		url: get_info_by_categories_ajax_url + "&shop_id=" + $("#shop").val() + "&site=" + $("#site").val() + "&category_id=" + $("#primary_category").val(),
		type: "get",
		dataType: "json",
		beforeSend: function(){
			$("#condition_id").attr("disabled", true);
			$(".condition-loading").html("<img src='img/loading.gif' />");
			$(".listing-type-loading").html("<img src='img/loading.gif' />");
			$(".item-specifics-loading").html("<img src='img/loading.gif' />");
		},
		success: function(data){
			if (data['features']['ack'] == SUCCESS)
			{
				if (data['features']['features']['variation_enabled'])
				{
					$("#MultiSKU_label").show();
				}
				else
				{
					$("#MultiSKU_label").hide();
				}
				$("#Chinese_radio").click();
				var _html_condition = '<option value="">--none--</option>';
				$.each(data['features']['features']['condition'], function(k, v){
					_html_condition += '<option value="' + v['id'] + '">' + v['display_name'] + '</option>';
				});
				$("#condition_id").html(_html_condition);
			}
			if (data['specifics']['ack'] == SUCCESS)
			{
				var _html_item_specifics = '';
				var _html_item_specifics_content = '';
				var _html_variation_specifics = '';
				var _html_variaiton_specifics_content = '';
				$.each(data['specifics']['specifics'], function(k, v){
					/* variation specifics */
					if (v['variation'])
					{
						_html_variation_specifics += '<label><input type="checkbox" value="variation_specifics_entry_' + k + '" title="' + v['name'] + '" class="variation-specifics-check" />' + v['name'] + '</label>';
						_html_variaiton_specifics_content += '<span id="variation_specifics_entry_' + k + '">';
						if ( ! $.isEmptyObject(v['recommendation']))
						{
							_html_variaiton_specifics_content += '<select style="width: 100px;margin-left: -16px;" onchange="$(this).next().val($(this).val());">';
							_html_variaiton_specifics_content += '<option></option>';
							$.each(v['recommendation'], function(kk, r){
								_html_variaiton_specifics_content += '<option value="' + r + '">' + r + '</option>';
							});
							_html_variaiton_specifics_content += '</select>';
							if (v['mode'] == 'SelectionOnly')
							{
								_html_variaiton_specifics_content += '<input type="text" style="margin-left: -100px;width: 80px;" readonly="readonly" />';
							}
							else
							{
								_html_variaiton_specifics_content += '<input type="text" style="margin-left: -100px;width: 80px;" />';
							}
						}
						else
						{
							if (v['mode'] == 'SelectionOnly')
							{
								_html_variaiton_specifics_content += '<input type="text" style="width: 80px;" readonly="readonly" />';
							}
							else
							{
								_html_variaiton_specifics_content += '<input type="text" style="width: 80px;" />';
							}
						}
						_html_variaiton_specifics_content += '</span>';
					}

					/* item specifics */
					if (v['min_value'] > 0)
					{
						_html_item_specifics += '<label><input type="checkbox" name="item_specifics[' + v['name'] + '][checked]" value="1" checked="checked" onclick="if($(this).is(\':checked\')){$(\'#item_specifics_entry_' + k + '\').show();}else{$(\'#item_specifics_entry_' + k + '\').hide();}" />' + v['name'] + '</label>';
					}
					else
					{
						_html_item_specifics += '<label><input type="checkbox" name="item_specifics[' + v['name'] + '][checked]" value="1" onclick="if($(this).is(\':checked\')){$(\'#item_specifics_entry_' + k + '\').show();}else{$(\'#item_specifics_entry_' + k + '\').hide();}" />' + v['name'] + '</label>';
					}
					_html_item_specifics_content += '<p id="item_specifics_entry_' + k + '" style="display: none;"><span>' + v['name'] + '</span><br />';
					if ( ! $.isEmptyObject(v['recommendation']))
					{
						_html_item_specifics_content += '<select style="width: 220px;" onchange="$(this).next().val($(this).val());">';
						_html_item_specifics_content += '<option></option>';
						$.each(v['recommendation'], function(kk, r){
							_html_item_specifics_content += '<option value="' + r + '">' + r + '</option>';
						});
						_html_item_specifics_content += '</select>';
						if (v['mode'] == 'SelectionOnly')
						{
							_html_item_specifics_content += '<input type="text" name="item_specifics[' + v['name'] + '][value]" style="margin-left: -220px; width: 200px;" readonly="readonly" />';
						}
						else
						{
							_html_item_specifics_content += '<input type="text" name="item_specifics[' + v['name'] + '][value]" style="margin-left: -220px;width: 200px;" />';
						}
					}
					else
					{
						if (v['mode'] == 'SelectionOnly')
						{
							_html_item_specifics_content += '<input type="text" name="item_specifics[' + v['name'] + '][value]" style="width: 200px;" readonly="readonly" />';
						}
						else
						{
							_html_item_specifics_content += '<input type="text" name="item_specifics[' + v['name'] + '][value]" style="width: 200px;" />';
						}
					}
					_html_item_specifics_content += '</p>';
				});
				$("#default_item_specifics").html(_html_item_specifics);
				$("#default_item_specifics_content").html(_html_item_specifics_content);
				$("#MultiSKU_variation_specifics").html(_html_variation_specifics);
				$("#MultiSKU_variation_specifics_content").html(_html_variaiton_specifics_content);
			}
		},
		complete: function(){
			$("#condition_id").attr("disabled", false);
			$(".condition-loading").html("");
			$(".listing-type-loading").html("");
			$(".item-specifics-loading").html("");
		}
	});
};
$(function(){
	$(".folder").colorbox({width:"30%", height:"70%", iframe:true});
	$(".datepicker").datepicker({dateFormat: 'yy-mm-dd', changeMonth: true, minDate: 0});
	$("#shop").change(function(){
		$(this).find("option[value='']").remove();
		if ($("#shop").val() && $("#site").val())
		{
			refresh_by_shop_and_site();
		}
	});
	$("#site").change(function(){
		$(this).find("option[value='']").remove();
		$(".browse-ebay-category-btn").each(function(){
			$(this).prev().val("");
			$(this).next().text("");
		});
		refresh_by_site();
		if ($("#shop").val() && $("#site").val())
		{
			refresh_by_shop_and_site();
		}
		$("#ebay_category_search_input").autocomplete(search_category_ajax_url + "&site=" + $(this).val()).result(function(event, data, formatted){if (data[0]){	$("#categories_select_id").text(data.id);$("#categories_select_name").text(data.name);}});
	});
	$(".browse-ebay-category-btn").colorbox({
		width:"900px",
		height:"600px",
		inline: true,
		onOpen: function(){
			if ( ! $("#site").val() || ! $("#shop").val())
			{
				$.colorbox.close();
				alert('Choose Shop and Market');
				return false;
			}
			$("#ebay_category_search_input").val("");
			$("#ebay_categories_callback_btn").val($(this).attr("id"));
			$("#categories_select_id").text($(this).prev().val());
			$("#categories_select_name").text($(this).next().text());
			$("#select_ebay_category_box").show();
			$("#categories_0").val("0").change();
		},
		onClosed: function(){$("#select_ebay_category_box").hide();}
	});
	$(".categories-select").change(function(){
		select_categories($(this));
	});
	$("#select_ebay_category_cancel_btn").click(function(){$("#select_ebay_category_box").hide();$.colorbox.close();$("#ebay_category_search_input").val("");});
	$("#select_ebay_category_select_btn").click(function(){
		if ($("#ebay_categories_callback_btn").val() == 'browse_ebay_primary_category_btn')
		{
			if ($("#primary_category").val() != $("#categories_select_id").text())
			{
				$("#primary_category").val($("#categories_select_id").text()).change();
			}
		}
		$("#" + $("#ebay_categories_callback_btn").val()).prev().val($("#categories_select_id").text());
		$("#" + $("#ebay_categories_callback_btn").val()).next().html($("#categories_select_name").text()).next().val($("#categories_select_name").text());
		$("#select_ebay_category_box").hide();
		$.colorbox.close();
		$("#ebay_category_search_input").val("");
	});
	$("#primary_category").change(function(){
		if ($(this).val())
		{
			refresh_by_category();
		}
	});
	$("#title").change(function(){
		if ($(this).val().length > 80)
		{
			$(this).next().html('Max Limit 80 chars. Now ' + $(this).val().length + ' chars');
		}
		else
		{
			$(this).next().html('');
		}
	});
	$("#condition_id").change(function(){
		$(this).prev().val($(this).find("option:selected").text());
		if ($(this).val() >= '1000' && $(this).val() <= '1499')
		{
			$("#condition_description").val("").attr("readonly", true);
		}
		else
		{
			$("#condition_description").attr("readonly", false);
		}
	});
	$("#Chinese_radio").click(function(){$(".listing-type-body").hide();$("#Chinese_tbody").show();$("#MultiSKU_variation_pictures_tbody, #MultiSKU_variation_pictures_content").hide();});
	$("#FixedPriceItem_radio").click(function(){$(".listing-type-body").hide();$("#FixedPriceItem_tbody").show();$("#MultiSKU_variation_pictures_tbody, #MultiSKU_variation_pictures_content").hide();});
	$("#MultiSKU_radio").click(function(){$(".listing-type-body").hide();$("#MultiSKU_tbody").show();$("#MultiSKU_variation_pictures_tbody, #MultiSKU_variation_pictures_content").show();});
	$("#add_custom_item_specifics_btn").click(function(){$("#custom_item_specifics_content").append('<p>Name: <input type="text" name="custom_item_specifics[add' + custom_item_specifics_no + '][name]" /> Value: <input type="text" name="custom_item_specifics[add' + custom_item_specifics_no + '][value]" /> <a href="javascript:;" class="del-custom-item-specifics-btn">Del</a></p>');custom_item_specifics_no++;});
	$(".del-custom-item-specifics-btn").live("click", function(){$(this).parent().remove();});
	$("#MultiSKU_variation_specifics_add_btn").click(function(){
		$(this).hide();
		$("#MultiSKU_variation_specifics_add_text").show().focus();
	});
	$("#MultiSKU_variation_specifics_add_text").blur(function(){
		var text = $.trim($(this).val());
		if (text != '')
		{
			$("#MultiSKU_variation_content > thead > tr > th:last").before('<th><span class="MultiSKU-variation-specifics-text">' + text + '</span> <a href="javascript:;" class="MultiSKU-variation-specifics-del-btn">Del</a></th>');
			$("#MultiSKU_variation_content > tbody").find("tr").each(function(){
				$(this).find("td:last").before('<td><input type="text" name="variation[' + $(this).find("td:last .variation-index").val() + '][specifics][' + text + ']" style="width: 80px;" /></td>');
			});
		}
		$(this).val("").hide();
		$("#MultiSKU_variation_specifics_add_btn").show();
	});
	$(".MultiSKU-variation-specifics-del-btn").live("click", function(){
		var index = $(this).parent().index();
		var text = $(this).prev().text();
		$(this).parent().remove();
		$("#MultiSKU_variation_content > tbody").find("tr").each(function(){
			$(this).find("td:eq(" + index + ")").remove();
		});
		$(".variation-specifics-check").each(function(){
			if ($(this).attr("title") == text)
			{
				$(this).attr("checked", false);
			}
		})
	});
	$("#add_variation_btn").click(function(){
		var variation_last_col = $("#MultiSKU_variation_content > thead > tr th").length - 1;
		$("#MultiSKU_variation_content > tbody").append('<tr>' + $("#MultiSKU_variation_content > tbody > tr:eq(0)").html() + '</tr>');
		$("#MultiSKU_variation_content > tbody > tr:last td").each(function(){
			if ($(this).index() != variation_last_col)
			{
				var text = $("#MultiSKU_variation_content > thead > tr th:eq(" + $(this).index() + ") .MultiSKU-variation-specifics-text").text();
				var first_line_value = $("#MultiSKU_variation_content > tbody > tr:eq(0) td:eq(" + $(this).index() + ") input").val();
				if ($(this).index() <= 2)
				{
					$(this).find("input").attr("name", "variation[add" + variaiont_index + "][" + text + "]").val(first_line_value);
				}
				else
				{
					$(this).find("input").attr("name", "variation[add" + variaiont_index + "][specifics][" + text + "]").val(first_line_value);
				}
			}
			else
			{
				$(this).html('<a href="javascript:;" onclick="$(this).parent().parent().remove();">Del</a><input type="hidden" class="variation-index" value="add' + variaiont_index + '" />');
			}
		});
		variaiont_index++;
	});
	$(".variation-specifics-check").live("click", function(){
		var title = $(this).attr("title");
		var exist_flag = false;
		var exist_index = '';
		var tag_id = $(this).val();
		$("#MultiSKU_variation_content > thead > tr th").each(function(){
			if ($(this).find(".MultiSKU-variation-specifics-text").text() == title)
			{
				exist_flag = true;
				exist_index = $(this).index();
			}
		});
		if ($(this).is(":checked") && ! exist_flag)
		{
			$("#MultiSKU_variation_content > thead > tr > th:last").before('<th width="100"><span class="MultiSKU-variation-specifics-text">' + title + '</span><a href="javascript:;" class="MultiSKU-variation-specifics-del-btn">Del</a></th>');
			$("#MultiSKU_variation_content > tbody").find("tr").each(function(){
				var index = $(this).find("td:last .variation-index").val();
				$(this).find("td:last").before('<td><span>' + $("#" + tag_id).html() + '</span></td>');
				$(this).find("td:eq(-2) input").attr("name", "variation[" + index + "][specifics][" + title + "]");
			});
		}
		if ( ! $(this).is(":checked") && exist_flag)
		{
			$("#MultiSKU_variation_content > thead > tr > th:eq(" + exist_index + ")").remove();
			$("#MultiSKU_variation_content > tbody").find("tr").each(function(){
				$(this).find("td:eq(" + exist_index + ")").remove();
			});
		}
	});
	$("#MultiSKU_variation_pictures_specifics_refresh_btn").click(function(){
		var old_value = $("#MultiSKU_variation_pictures_specifics_refresh_select").val();
		var exist_flag = false;
		var variation_last_col = $("#MultiSKU_variation_content > thead > tr th").length - 1;
		var _html = '<option value="">--none--</option>';
		$("#MultiSKU_variation_content > thead  tr th").each(function(){
			if ($(this).index() > 2 && $(this).index() < variation_last_col)
			{
				if (old_value == $(this).find(".MultiSKU-variation-specifics-text").text())
				{
					exist_flag = true;
				}
				_html += '<option value="' + $(this).find(".MultiSKU-variation-specifics-text").text() + '">' + $(this).find(".MultiSKU-variation-specifics-text").text() + '</option>';
			}
		});
		$("#MultiSKU_variation_pictures_specifics_refresh_select").html(_html);
		if (exist_flag)
		{
			$("#MultiSKU_variation_pictures_specifics_refresh_select").val(old_value).change();
		}
		else
		{
			$("#MultiSKU_variation_pictures_specifics_refresh_select").change();
		}
	});
	$("#MultiSKU_variation_pictures_specifics_refresh_select").change(function(){
		var specifics = $(this).val();
		var exist_flag = false;
		var exist_index = '';
		$("#MultiSKU_variation_content > thead > tr th").each(function(){
			if ($(this).find(".MultiSKU-variation-specifics-text").length > 0 && $(this).find(".MultiSKU-variation-specifics-text").text() == specifics)
			{
				exist_flag = true;
				exist_index = $(this).index();
			}
		});
		var _html = '';
		if (exist_flag)
		{
			var id_arr = new Array();
			$("#MultiSKU_variation_content > tbody").find("tr").each(function(){
				var text = $.trim($(this).find("td:eq(" + exist_index + ") input").val());
				var index = $(this).find("td:last input").val();
				var id = hex_md5('variation_pictures_' + specifics + '_' + text);
				if ($.inArray(id, id_arr) == -1)
				{
					id_arr.push(id);
					if ($("#" + id).length == 0)
					{
						_html += '<tr id="' + id + '">';
						_html += '<td><strong>' + text + '</strong>';
						_html += '</td><td align="left">';
						_html += '<input type="text" name="variation_picture[' + text + '][url]" size="100" />';
						_html += '</td></tr>';
					}
				}
			});
			$("#MultiSKU_variation_pictures_content tr").each(function(){
				if ($.inArray($(this).attr("id"), id_arr) == -1)
				{
					$(this).remove();
				}
			});
			$("#MultiSKU_variation_pictures_content").append(_html);
		}
		else
		{
			$("#MultiSKU_variation_pictures_content").html("");
		}
		$("#MultiSKU_variation_pictures_content").show();
	});
	$("#preview_btn").click(function(){
		var _html = $("#description").val();
		open_window = window.open("", "newwin", "width=900,height=600,toolbar=no,scrollbars=yes");
		open_window.document.open();
		open_window.document.write(_html);
		open_window.document.close();
	});
	$(".shipping-free-checkbox").live("click", function(){
		if ($(this).is(":checked"))
		{
			$(this).parent().parent().find("input[type='text']").attr("disabled", true);
		}
		else
		{
			$(this).parent().parent().find("input[type='text']").attr("disabled", false);
		}
	});
	$(".shipping-service-location-select").live("change", function(){
		if ($(this).val() == 'Custom')
		{
			var index = $(this).attr("title");
			var _html_shipping_location_option = '';
			$.each(shipping_location, function(k, v){
				_html_shipping_location_option += '<label style="float: left;"><input type="checkbox" name="international_shipping[' + index + '][location_custom][]" value="' + v['value'] + '" />' + v['name'] + '</label>';
			});
			$(this).parent().find(".shipping-service-location-content").html(_html_shipping_location_option);
		}
		else
		{
			$(this).parent().find(".shipping-service-location-content").html("");
		}
	});
	$("#add_domestic_shipping_service_btn").click(function(){
		var _html_domestic_shipping_service_content = '<tr>';
		_html_domestic_shipping_service_content += '<th>Service <a href="javascript:;" class="shipping-service-del-btn">Del</a></th><td align="left"><select name="domestic_shipping[add' + domestic_shipping_index + '][service]">' + domestic_shipping_service_option_html + '</select>';
		_html_domestic_shipping_service_content += ' Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="domestic_shipping[add' + domestic_shipping_index + '][cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
		_html_domestic_shipping_service_content += ' Additional Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="domestic_shipping[add' + domestic_shipping_index + '][additional_cost]" size="7" ><span class="currency_code">' + currency_code + '</span>';
		_html_domestic_shipping_service_content += ' <label><input type="checkbox" name="domestic_shipping[add' + domestic_shipping_index + '][free]" value="' + is_free + '" class="shipping-free-checkbox" />Free</label>';
		_html_domestic_shipping_service_content += '</td></tr>';
		$("#domestic_shipping_service_content").append(_html_domestic_shipping_service_content);
		domestic_shipping_index++;
	});
	$("#add_international_shipping_service_btn").click(function(){
		var _html_international_shipping_service_content = '<tr>';
		_html_international_shipping_service_content += '<th>Service <a href="javascript:;" class="shipping-service-del-btn">Del</a></th><td align="left"><select name="international_shipping[add' + international_shipping_index + '][service]">' + international_shipping_service_option_html + '</select>';
		_html_international_shipping_service_content += ' Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="international_shipping[add' + international_shipping_index + '][cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
		_html_international_shipping_service_content += ' Additional Cost: <span class="currency_symbol">' + currency_symbol + '</span><input type="text" name="international_shipping[add' + international_shipping_index + '][additional_cost]" size="7"><span class="currency_code">' + currency_code + '</span>';
		_html_international_shipping_service_content += ' <label><input type="checkbox" name="international_shipping[add' + international_shipping_index + '][free]" value="' + is_free + '" class="shipping-free-checkbox" />Free</label><hr style="margin: 4px 0;" />';
		_html_international_shipping_service_content += 'Location: <select name="international_shipping[add' + international_shipping_index + '][location]" title="add' + international_shipping_index + '" class="shipping-service-location-select"><option value="Worldwide">Worldwide</option><option value="Custom">Custom</option></select><br /><div class="shipping-service-location-content"></div>';
		_html_international_shipping_service_content += '</td></tr>';
		$("#international_shipping_service_content").append(_html_international_shipping_service_content);
		international_shipping_index++;
	});
	$(".shipping-service-del-btn").live("click", function(){$(this).parent().parent().remove();});
	$(".return-policy-accepted-radio").live("click", function(){
		if ($(this).val() == 'ReturnsAccepted')
		{
			$("#return_policy_content").show();
		}
		else
		{
			$("#return_policy_content").hide();
		}
	});
	$("#timezone,#minute,#date,#hour").change(function(){
		if($("#date").val())
		{
			var hour= ($("#hour").val() == "")?0:$("#hour").val();
			var time_str = $("#date").val()+"T"+hour+":"+$("#minute").val()+":00+08:00";
			datetime = new Date(time_str);
			utc = datetime.getTime() + (datetime.getTimezoneOffset() * 60000);
			nd = new Date(utc + (3600000*($("#timezone").val()/100)));
			M = (((nd.getMonth()+1) < 10) ? ('0'+(nd.getMonth()+1)) : (nd.getMonth()+1)) ;
			D = (nd.getDate() < 10 ? '0'+nd.getDate() : nd.getDate());
			H = (nd.getHours() < 10 ? '0'+nd.getHours() : nd.getHours()) + ':';
			Min = (nd.getMinutes() < 10 ? '0'+nd.getMinutes() : nd.getMinutes());
			$("#show_time").text(nd.getFullYear()+"-"+M+"-"+D+" "+H+Min);
		}
	});
	$("#timezone").change();
	$("#submit_btn").click(function(){$(this).attr("disabled", true);$("#submit_form").attr("action", submit_url).submit();});
	$("#dialog_verify_info").dialog({autoOpen: false, modal: true, height: 400, width: 700});
	$("#verify_btn").click(function(){
		var current_tag = $(this);
		current_tag.attr("disabled", true);
		current_tag.next().show();
		$("#submit_form").attr("action", verify_url);
		$("#submit_form").ajaxSubmit({
			dataType: 'json',
			success: function(data){
				current_tag.attr("disabled", false);
				current_tag.next().hide();
				if (data['ack'] == SUCCESS)
				{
					$("#verify_ack").html(data['verify_result']['ack']);
					$("#verify_fee").html(data['verify_result']['fee']);
					var _html_errors = '';
					$.each(data['verify_result']['errors'], function(k, v){
						_html_errors += '<strong>Error' + (k + 1) + ': </strong>' + v + '<br />';
					});
					$("#verify_errors").html(_html_errors);
					$("#dialog_verify_info").dialog("open");
				}
				else
				{
					$("#verify_ack").html("");
					$("#verify_fee").html("");
					$("#verify_errors").html("");
				}
			}
		});
	});
});